package com.caseProcess.controller;

import com.alibaba.fastjson.JSONObject;
import com.caseProcess.entity.*;
import com.caseProcess.service.CaseOperNoteService;
import com.caseProcess.utils.IdWorker;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/caseOperNote")
public class CaseOperNoteController {

    @Resource
    private CaseOperNoteService caseOperNoteService;

    IdWorker idWorker=new IdWorker(1,1,1);

    /**
     * 添加过程
     * @param processId 分派id
     * @param staticId 状态id
     * @param procRemark 内容
     * @param nextDepartmentName 下一个处理者所在部门名称
     * @param nextPoliceId 下一个处理者
     * @param request
     * @return
     * @throws Exception
     */
    @Transactional
    @RequestMapping("/insert")
    public Result insert(String processId,long staticId,String procRemark,String nextDepartmentName,String nextPoliceId,HttpServletRequest request) throws Exception {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        CaseOperNote caseOperNote=new CaseOperNote();
        CaseProcInfo caseProcInfo=new CaseProcInfo();
        caseProcInfo.setProcessId(processId);
        caseOperNote.setCaseProcInfo(caseProcInfo);
        List<CaseOperNote> list=new ArrayList<CaseOperNote>();
        list=caseOperNoteService.selectCaseOperNotes(caseOperNote);
        if(list.size()<1){
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("指派信息查询异常");
            return result;
        }
        CaseOperNote previousCON=list.get(list.size()-1);
        caseOperNote.setOperNoteId(idWorker.nextId()+"");
        caseOperNote.setCaseProcInfo(previousCON.getCaseProcInfo());
        caseOperNote.setSortNum(1);
        caseOperNote.setParentId(previousCON.getOperNoteId());
        User user=new User();
        VUserInfo vUserInfo= (VUserInfo) object;
        user.setPoliceId(vUserInfo.getPoliceId());
        caseOperNote.setProcUser(user);
        caseOperNote.setDepartmentName(vUserInfo.getDepartmentName());
        ProcessStatus processStatus=new ProcessStatus();
        processStatus.setStaticId(staticId);
        caseOperNote.setProcessStatus(processStatus);
        caseOperNote.setResources(false);
        caseOperNote.setSubmit(true);
        caseOperNote.setNextDepartmentName(nextDepartmentName);
        User nextUser=new User();
        nextUser.setPoliceId(nextPoliceId);
        caseOperNote.setNextUser(nextUser);
        caseOperNote.setProcRemark(procRemark);
        caseOperNoteService.insert(caseOperNote);
        result.setMsg("操作成功");

        return result;
    }

    /**
     * 添加第一步过程(派出所指派到民警)
     * @param processId 分派id
     * @param procRemark //本警情已指派给fullNAme(roleName)开始处理
     * @param nextPoliceId 下一个处理者
     * @param request
     * @return
     * @throws Exception
     */
    @Transactional
    @RequestMapping("/insertOne")
    public Result insertOne(String processId,String procRemark,String nextPoliceId,HttpServletRequest request) throws Exception {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        CaseOperNote caseOperNote=new CaseOperNote();
        CaseProcInfo caseProcInfo=new CaseProcInfo();
        caseProcInfo.setProcessId(processId);
        caseOperNote.setCaseProcInfo(caseProcInfo);
        caseOperNote.setOperNoteId(idWorker.nextId()+"");
        caseOperNote.setCaseProcInfo(caseProcInfo);
        caseOperNote.setSortNum(1);
        caseOperNote.setParentId("0");
        User user=new User();
        VUserInfo vUserInfo= (VUserInfo) object;
        user.setPoliceId(vUserInfo.getPoliceId());
        caseOperNote.setProcUser(user);
        caseOperNote.setDepartmentName(vUserInfo.getDepartmentName());
        ProcessStatus processStatus=new ProcessStatus();
        processStatus.setStaticId(1);//待处理(派出所指派到民警)
        caseOperNote.setProcessStatus(processStatus);
        caseOperNote.setResources(false);
        caseOperNote.setSubmit(true);
        caseOperNote.setNextDepartmentName(vUserInfo.getDepartmentName());
        User nextUser=new User();
        nextUser.setPoliceId(nextPoliceId);
        caseOperNote.setNextUser(nextUser);
        caseOperNote.setProcRemark(procRemark);
        caseOperNoteService.insert(caseOperNote);
        result.setMsg("操作成功");

        return result;
    }


    @Transactional
    @RequestMapping("/insertAndFile")
    public Result insertAndFile(String processId,String procRemark,int start,HttpServletRequest request) throws Exception {
        Result result=new Result();
        Object object=SessionController.getSession(request,"police");
        if(object==null){
            result.setCode(-500);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }
        CaseOperNote caseOperNote=new CaseOperNote();
        CaseProcInfo caseProcInfo=new CaseProcInfo();
        caseProcInfo.setProcessId(processId);
        caseOperNote.setCaseProcInfo(caseProcInfo);
        List<CaseOperNote> list=new ArrayList<CaseOperNote>();
        list=caseOperNoteService.selectCaseOperNotes(caseOperNote);
        if(list.size()<1){
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("指派信息查询异常");
            return result;
        }
        CaseOperNote previousCON=list.get(list.size()-1);
        caseOperNote.setOperNoteId(idWorker.nextId()+"");
        caseOperNote.setCaseProcInfo(previousCON.getCaseProcInfo());
        caseOperNote.setSortNum(1);
        caseOperNote.setParentId(previousCON.getOperNoteId());
        User user=new User();
        VUserInfo vUserInfo= (VUserInfo) object;
        user.setPoliceId(vUserInfo.getPoliceId());
        caseOperNote.setProcUser(user);
        caseOperNote.setDepartmentName(vUserInfo.getDepartmentName());
        ProcessStatus processStatus=new ProcessStatus();
        if (start == 1){
            processStatus.setStaticId(3);
            caseOperNote.setNextDepartmentName("指挥中心");
        }else{
            processStatus.setStaticId(2);
            caseOperNote.setNextDepartmentName("");
        }
        caseOperNote.setProcessStatus(processStatus);
        caseOperNote.setResources(false);
        caseOperNote.setSubmit(true);
        caseOperNote.setProcRemark(procRemark);
        if (start == 1){
            caseOperNoteService.insertAndFile(caseOperNote);
        }else{
            caseOperNoteService.insertAndFileTwo(caseOperNote);
        }
        return result;
    }

    /**
     * 获取申请人信息
     * @param caseOperJson 条件
     * @return 申请人信息
     */
    @RequestMapping("/getTapplicantInfo")
    private Result getTapplicantInfo(String caseOperJson){
        CaseOperNote caseOperNote = JSONObject.parseObject(caseOperJson, CaseOperNote.class);
        List<CaseOperNote> caseOperNotes = caseOperNoteService.selectCaseOperNotes(caseOperNote);
        Result result = new Result();
        if(caseOperNotes.size() > 0){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(caseOperNotes.get(caseOperNotes.size()-1));
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     * 指挥中心审批
     * @param caseOperNoteJson 审批信息
     * @param request
     * @return 操作结果
     */
    @RequestMapping("insertCheck")
    private Result insertCheck(String caseOperNoteJson,HttpServletRequest request){
        CaseOperNote caseOperNote = JSONObject.parseObject(caseOperNoteJson, CaseOperNote.class);
        Result result = new Result();
        //分派Id
        String procId = caseOperNote.getCaseProcInfo().getProcessId();
        //状态Id
        Long status = caseOperNote.getProcessStatus().getStaticId();
        //意见
        String remark = caseOperNote.getProcRemark();
        //下一个处理人员
        String nextPoliec = "";
        //下一个处理部门
        String nextDepartment = "";

        if (status == 5){
            nextPoliec = "";
            nextDepartment = "";
        }else if(status == 4){
            nextPoliec = caseOperNote.getNextUser().getPoliceId();
            nextDepartment = caseOperNote.getNextDepartmentName();
        }else{
            CaseOperNote caseOper = new CaseOperNote();
            CaseProcInfo caseProcInfo = new CaseProcInfo();
            caseProcInfo.setProcessId(procId);
            caseOper.setCaseProcInfo(caseProcInfo);
            List<CaseOperNote> caseOperNotes = caseOperNoteService.selectCaseOperNotes(caseOper);
            if(caseOperNotes.size() >= 0){
                nextPoliec = caseOperNotes.get(0).getNextUser().getPoliceId();
                nextDepartment = caseOperNotes.get(0).getNextDepartmentName();
            }else{
                result.setCode(Result.RESULT_ERROR);
                result.setMsg("操作失败");
                return result;
            }
        }

        try {
            result = insert(procId, status, remark, nextDepartment, nextPoliec, request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
